<?php

class Grade_identification extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Grade_identification':
	 * @var integer $id
	 * @var integer $fk_truck_id
	 * @var string $grade_identify_date
	 * @var string $next_grade_identify_date
	 * @var double $grade_identify_cost
	 * @var double $other_cost
	 * @var double $total_cost
	 * @var integer $state
	 * @var string $create_time
	 */
	
	public $property_owner;
	public $plate_no;
	private $_kind=array('0'=>'车辆','1'=>'尾板');

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'grade_identification';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('fk_truck_id, state', 'numerical', 'integerOnly'=>true),
			array('remark','length','max'=>500),
			array('grade_identify_cost, other_cost, total_cost', 'numerical'),
			array('property_owner,plate_no,grade_identify_cost,boxed_cost, other_cost, total_cost,grade_identify_date,next_grade_identify_date', 'required'),
			array('grade_identify_cost, boxed_cost, other_cost, total_cost', 'match','pattern' => '/^\d{1,16}(\.\d{1,2})?$/',
			'message'=>'{attribute} 整数位最多16位，小数位2位'),
			array('grade_identify_date,next_grade_identify_date', 'type','type'=>'date','message'=>'{attribute} 必须为日期型(年-月-日)'),
		);
	}
	
	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)
	{
		if($this->isNewRecord){
			$this->state=0;
			$this->create_time=date('Y-m-d G:i:s');
		}
		return true;
	}
	
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '索引号',
			'property_owner' => '产权单位',
			'plate_no' => '车辆/尾板牌号',
			'fk_truck_id' => '车辆/尾板Id',
			'kind' =>'车辆/尾板',
			'grade_identify_date' => '等级鉴定日期',
			'next_grade_identify_date' => '下次等级鉴定日期',
			'grade_identify_cost' => '等级鉴定费(元)',
			'boxed_cost' => '吊箱费(元)',
			'other_cost' => '其他费用(元)',
			'total_cost' => '总费用(元)',
			'remark'=>'备注',
			'state' => '状态',
			'create_time' => '创建时间',
		);
	}
	
	public function excelAttributes(){//导出excel所需的字段名、列号、宽度
		return array(
			'plate_no'=>array('B',15),
			'property_owner'=>array('C',10),
			'kind'=>array('D',15,'getKindName'),
			'grade_identify_date'=>array('E',15),
			'next_grade_identify_date'=>array('F',20),
			'grade_identify_cost'=>array('G',15),
			'boxed_cost'=>array('H',15),
			'other_cost'=>array('I',15),
			'total_cost'=>array('J',15),
			'create_time'=>array('K',20),
		);
	}
	
	public function getKindNames(){
		return $this->_kind;
	}
	
	public function getKindName($n=null){
		return $this->_kind[$n==null?$this->kind:$n];
	}
	
	public function safeAttributes(){
		$params=parent::safeAttributes();
		$params[]='property_owner';
		$params[]='plate_no';
		return $params;
	}
}